package digital.store.springmvc.controller;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import digital.store.springmvc.service.OrderService;
import digital.store.tutorial.springmvc.model.Order;

@Controller
@RequestMapping(value = "/order")
public class OrderController {
	 @Autowired
	 private OrderService orderService;

	 @RequestMapping(method = RequestMethod.GET)
	 public String list(Model model) {
	        Collection<Order> orders = orderService.findAll();
	        model.addAttribute("orders", orders);
	       return "admin/order/order-list";
	    }
	 
	 @RequestMapping(value="save", method = RequestMethod.POST)
	    public String save(@Valid Order order, BindingResult result,HttpServletRequest request, HttpServletResponse response,
				HttpSession session) {
	        if (result.hasErrors()) {
	            return "admin/order/create-edit-o";
	        }
	        orderService.saveOrUpdate(order);
	        return "redirect:/admin/order/";
	    }
}
